package com.taptech.doufu.presenter.home;

import android.app.Activity;
import android.content.Context;

import com.google.gson.Gson;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.util.CacheUtil;
import com.taptech.doufu.bean.novel.ChatNovelTopicBean;
import com.taptech.doufu.bean.novel.ChatNovelTopicGsonBean;
import com.taptech.doufu.listener.CommmonImp;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.util.GsonUtil;

import org.json.JSONObject;

import java.io.File;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Wyatt on 2017/10/13/013.
 */

public class ChatNovelTopicPresenter {
    public interface OnHomeTopicView{
        void onTopicNext(List<ChatNovelTopicBean.Node> list, boolean isCache);
        void onTopicError();
    }

    private OnHomeTopicView onHomeTopicView;
    private Context mContext;

    public ChatNovelTopicPresenter(Context context,OnHomeTopicView onHomeTopicView) {
        this.onHomeTopicView = onHomeTopicView;
        mContext = context;
    }

    /**
     * 获取对话小说缓存
     */
    public void loadChatNovelTopicCache() {
        Observable.create(new Observable.OnSubscribe<ChatNovelTopicGsonBean>() {
                    @Override
                    public void call(Subscriber<? super ChatNovelTopicGsonBean> subscriber) {
                        JSONObject json = CacheUtil.readJsonDataFromCache(new File(CommmonImp.Home_novel_chat_list));
                        ChatNovelTopicGsonBean node = GsonUtil.parseJsonWithGson(json.toString(), ChatNovelTopicGsonBean.class);
                        subscriber.onNext(node);
                    }
                })
                .compose(RxJavaHelper.<ChatNovelTopicGsonBean>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<ChatNovelTopicGsonBean>(){
                    @Override
                    public void onNext(ChatNovelTopicGsonBean node) {
                        super.onNext(node);
                        if (onHomeTopicView != null && node!=null&& node.getData()!=null) {
                            onHomeTopicView.onTopicNext(node.getData().getList(),true);
                        }
                    }
                });
    }

    /**
     * 获取话题数据
     */
    public void getChatNovelTopic(boolean isUseCache,boolean isFirstLoad) {
        int firstLoad = 0;
        if (isFirstLoad) {
            firstLoad = 1;
        }
        ApiClient.getInstance().setUseCache(isUseCache).getService().getChatNovelTopic(firstLoad)
                .map(new Func1<ChatNovelTopicGsonBean, ChatNovelTopicGsonBean>() {
                    @Override
                    public ChatNovelTopicGsonBean call(ChatNovelTopicGsonBean responseCpIndexDataBean) {
                        CacheUtil.saveJsonDataToCache(new Gson().toJson(responseCpIndexDataBean),new File(CommmonImp.Home_novel_chat_list));
                        return responseCpIndexDataBean;
                    }
                })
                .compose(RxJavaHelper.<ChatNovelTopicGsonBean>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<ChatNovelTopicGsonBean>(){
                    @Override
                    public void onNext(ChatNovelTopicGsonBean gsonBean) {
                        super.onNext(gsonBean);
                        if (onHomeTopicView != null) {
                            onHomeTopicView.onTopicNext(gsonBean.getData().getList(),false);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (onHomeTopicView != null) {
                            onHomeTopicView.onTopicError();
                        }
                    }
                });
    }

}
